﻿var dataDepart = "";
$(function () {
    //$("#btnAdd").click(function () { add(0); });
    $("#btnSave").click(function () { save(); }); 
    loadDepartment();
    loadTables(1);
});

//加载列表数据
function loadTables(pageIndex) {
    $("#tableBody").html("");
    $("#checkAll").prop("checked", false);
    $.ajax({
        type: "GET",
        url: "/Gzh/YYSM/GetPagedListToJson?pageindex=" + pageIndex + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data.rows, function (i, item) {
                var tr = "<tr>";
                tr += "<td align='center'>" + item.id + "</td>";
                tr += "<td>" + findDepartmentNameByDepartmentId(item.departmentId) + "</td>";
                tr += "<td>" + item.name + "</td>";
                tr += "<td>" + item.tel + "</td>";
                tr += "<td>" + item.bzType + "</td>";
                tr += "<td>" + item.yYtime + "</td>";
                tr += "<td>" + (item.isBL == true ? "已办" : "未办") + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='edit(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteSingle(\"" + item.id + "\")'><i class='fa fa-trash-o'></i> </button> </td>"
                tr += "</tr>";
                $("#tableBody").append(tr);
            })
            var elment = $("#grid_paging_part"); //分页插件的容器id
            if (data.rowCount > 0) {
                var options = { //分页插件配置项
                    bootstrapMajorVersion: 3,
                    currentPage: pageIndex, //当前页
                    numberOfPages: data.rowCount, //总数
                    totalPages: data.pageCount, //总页数
                    onPageChanged: function (event, oldPage, newPage) { //页面切换事件
                        loadTables(newPage);
                    },
                    itemTexts: function (type, page, current) {
                        switch (type) {
                            case "first": return "首页";
                            case "prev": return "上一页";
                            case "next": return "下一页";
                            case "last": return "末页";
                            case "page": return page;
                        }
                    }
                }
                elment.bootstrapPaginator(options); //分页插件初始化
            }
        }
    })
}

function loadDepartment() {   
    $.ajax({
        type: "GET",
        url: "/Sys/Department/GetAllListToJson",
        async: false,       //Ajax请求默认的都是异步的,此处设为同步
        success: function (data) {            
            dataDepart = data;           
        }
    });
}

function findDepartmentNameByDepartmentId(departmentId) {
    var result = "";
    $.each(dataDepart, function (i, item) {
        if (item.id == departmentId) {
            result = item.name;
            //alert(html);
            return result;
        }
    });
    return result;
}


//编辑
function edit(id) {
    $.ajax({
        type: "Get",
        url: "/Gzh/YYSM/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#CreatedBy").val(data.createdBy);
            $("#CreatedOn").val(data.createdOn);

            $("#Id").val(data.id);
            $("#DepartmentId").val(data.departmentId);
            $("#DepartmentId1").val(findDepartmentNameByDepartmentId(data.departmentId));
            $("#Name").val(data.name);
            $("#Tel").val(data.tel);
            $("#BZType").val(data.bzType);
            $("#Address").val(data.address);
            $("#YYtime").val(data.yYtime);
            $("#IsBL").prop("checked", data.isBL);
            $("#Remarks").val(data.remarks);

            $("#Title").text("编辑");
            $("#editModal").modal("show");
        }
    })
};
//保存
function save() {
    var postData = {
        "CreatedBy": $("#CreatedBy").val(),
        "CreatedOn": $("#CreatedOn").val(),

        "Id": $("#Id").val(),
        "DepartmentId": $("#DepartmentId").val(),
        "Name": $("#Name").val(),
        "Tel": $("#Tel").val(),
        "BZType": $("#BZType").val(),
        "Address": $("#Address").val(),
        "YYtime": $("#YYtime").val(),
        "IsBL": $("#IsBL").prop('checked'),
        "Remarks": $("#Remarks").val()
    };
    $.ajax({
        type: "Post",
        url: "/Gzh/YYSM/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadTables(1)
                $("#editModal").modal("hide");
            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};

//删除单条数据
function deleteSingle(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Gzh/YYSM/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadTables(1);
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！");
                }
            }
        })
    });
};

